﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public partial class TeacherList : SecurityPage
    {
        public override string PrivCode()
        {
            return "c7fd3b6dc2d30e7c";
        }

        private void TVBangding()
        {
            this.TV_Student.Nodes.Clear();
            List<xy_bmxbm> list = xy_bmxbm.Where("BMFQID=@0", "03").ToList();
            list.ForEach(p =>
            {
                TreeNode node = new TreeNode();
                node.Value = p.BMDM;
                node.Text = p.BMMC;
                node.Target = Convert.ToInt32(EnumData.TvNodeType.学院).ToString();
                this.AddChildNodes(node, p.BMDM);
                this.TV_Student.Nodes.Add(node);
            });
            this.TV_Student.CollapseAll();
        }

        private void AddChildNodes(TreeNode ParentNode, string BMDM)
        {
            List<xy_bmxbm> list = xy_bmxbm.Where("BMFQID=@0", BMDM).ToList();
            list.ForEach(p =>
            {
                TreeNode node = new TreeNode();
                node.Value = p.BMDM;
                node.Text = p.BMMC;
                node.Target = Convert.ToInt32(EnumData.TvNodeType.部门).ToString();
                //this.AddChildNodes(node, p.BMDM);  只找学院下面的第一级
                ParentNode.ChildNodes.Add(node);
            });
        }

        private void RepBanding()
        {
            if (this.TV_Student.SelectedNode == null) return;

            var s = from A in this.TV_Student.SelectedNode.AllNodes()
                    where A.Target == Convert.ToInt32(EnumData.TvNodeType.部门).ToString()
                    select A.Value;
            var q = from B in LinqUtility.LinqContext.xy_jgxx
                    where s.Contains(B.BMDM)
                    select B;

            int RowCount = q.Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<xy_jgxx> list = q.Skip(SkipCount).Take(PageSize).ToList();
            this.Rep_DataList.DataSource = list;
            this.Rep_DataList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            RepBanding();
            TVBangding();
        }

        protected void Rep_DataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string XH = Convert.ToString(e.CommandArgument);
            if (e.CommandName == "Del")
            {
                xy_jgxx.Delete("ZGH=@0", XH);
                UserList.Delete("XH=@0 and IsStudent=@1", XH, false);
                RepBanding();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepBanding();
        }

        protected void TV_Student_SelectedNodeChanged(object sender, EventArgs e)
        {
            RepBanding();
        }

        protected void RefreshList_Click(object sender, EventArgs e)
        {
            this.RepBanding();
        }
    }
}